package main

import "fmt"
import "time"

// Write data
func writeData(intChan chan int) {
	for i := 0; i < 50; i++ {
		// 放入数据
		intChan <- i
		fmt.Printf("writeData 写入数据=%v\n", i)
	}
	time.Sleep(time.Second)
	close(intChan)
}

// read data
func readData(intChan chan int, exitChan chan bool) {
	for {
		v, ok := <- intChan
		if !ok {
			break
		}
		fmt.Printf("readData 读到数据=%v\n", v)
	}
	time.Sleep(time.Second)
	// readData 读取完数据后，即任务完成
	exitChan <- true
	close(exitChan)
}

func main() {

	intChan := make(chan int, 50)
	exitChan := make(chan bool, 1)
	go writeData(intChan)
	go readData(intChan, exitChan)

	for {
		_, ok := <- exitChan
		if !ok {
			break
		}
	}
}